package com.xgpro.common.DemoTask;

import com.xgpro.common.utils.ReadPropertiesUtil;
import com.xgpro.mobile.message.service.MessageService;
import com.xgpro.mobile.message.service.UserMsgService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 定时任务(删除消息)
 * @author xy
 */
@Component
public class DemoScheduledTask {

    @Resource
    private MessageService messageService;
    @Resource
    private UserMsgService userMsgService;

    @Scheduled(cron="0 0 23 * * ?")
    public void test1() throws ParseException {

        String time= ReadPropertiesUtil.readUrl("delete_msg");
        int i = Integer.parseInt(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        Calendar cld = Calendar.getInstance();
        Date date = new Date();
        String formatDate1 = DateFormat.getDateInstance().format(date);
         date = sdf.parse(formatDate1);
        cld.setTime(date);
        cld.set(Calendar.DATE, cld.get(Calendar.DATE)-i);
        String format = sdf.format(cld.getTime());
        int i1 = messageService.deleteByTime(format);
        int i2 = userMsgService.deleteByTime(format);
        System.out.println("每天23点执行一次--删除"+i1+"条消息");
        System.out.println(format);



}
}
